﻿/// <reference path="jquery-1.8.2.min.js" />
/// <reference path="site.js" />
/// <reference path="animation.js" />

site.menu = {
    portfolio: {
        container: null, // something like $("menu>li.portfolio")[0],
        storyboard: null
    },
    experiments: {
        container: null, // something like $("menu>li.experiments")[0],
        storyboard: null
    },
    IsSelected: function (menuItem) {
        return $("body").hasClass($(menuItem).attr("class") + "-selection");
    },
    Select: function (menuItem) {
        var currentClass = $("body").attr("class");
        var itemNameToSelect = $(menuItem).attr("class");
        var wantedClass = itemNameToSelect + "-selection";
        
        // Check whether the item is not already selected
        if (currentClass != wantedClass) {
            
            // Check whether selected item should be removed
            if (currentClass && currentClass != 'undefined' && currentClass.length > 0) {
                var currentClasses = currentClass.split(" ");
                
                // Remove all selection classes
                for (var index in currentClasses) {
                    var splitIndex = currentClasses[index].indexOf("-selection");
                    if (splitIndex != -1) {
                        $("body").removeClass(currentClasses[index]);
                        var itemNameToUnselect = currentClasses[index].toString().substring(0, splitIndex);
                        var menuItemToUnselect = $("menu>li." + itemNameToUnselect);
                        menuItemToUnselect[0].storyboard.Begin(1);
                        
                        // Call the close method for unselected category
                        if (site[itemNameToUnselect]) site[itemNameToUnselect].Close();
                    }
                }
            }
            
            // Call the open method for selected category
            if (site[itemNameToSelect]) site[itemNameToSelect].Open();

            // Assign class
            $("body").addClass(wantedClass);
        }
    },

    // Menu initialization
    InitializeComponent: function () {
        site.menu.portfolio.container = $("menu>li.portfolio")[0];
        site.menu.portfolio.storyboard = new Storyboard(site.menu.portfolio.container, 250, 150);
        site.menu.portfolio.storyboard.PushRow(45);
        site.menu.portfolio.storyboard.PushRow(33);

        site.menu.portfolio.container = $("menu>li.experiments")[0];
        site.menu.portfolio.storyboard = new Storyboard(site.menu.portfolio.container, 250, 150);
        site.menu.portfolio.storyboard.PushRow(58);
        site.menu.portfolio.storyboard.PushRow(58);

        $("menu>li").mouseenter(function () {
            if (!site.menu.IsSelected(this)) {
                this.storyboard.Begin(0);
            }
        });

        $("menu>li").mouseleave(function () {
            if (!site.menu.IsSelected(this)) {
                if (this.storyboard.isRunning && this.storyboard.currentRow == 0) {
                    this.storyboard.isReverse = true;
                } else {
                    this.storyboard.Begin(1);
                }
            }
        });

        $("menu>li").click(function () {
            site.menu.Select(this);
        });
    }
};